package com.honey.compilation.sql.ddl;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlConstraint;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlType;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.utils.StringUtility;

/**
 * 创建table使用的column,主要是在SqlColumn基础上增加了数据库约束
 * 
 * @author Administrator
 *
 */
public class ExtendConstraintColumn extends SqlColumn{
	
	/** 数据库约束 */
	private SqlConstraint constraint = new ConstraintUndefine();

	/** SQL 92中约束有两种写法<br /> 
	 	一种写在列定义中<br />
	 	一种写在表的定义中<br />
	 	默认使用写在表的定义中
	 	
	 */
	private boolean constraintInline =false;
	
	/**
	 * 构造函数
	 * @param name 列的名称
	 */
	public ExtendConstraintColumn(String name) {
		super(name,null);
	}
	
	/**
	 * 构造函数
	 * @param name 列的名称
	 * @param jdbcType 在java.sql.Types类中指定类型
	 */
	public ExtendConstraintColumn(String name, int jdbcType) {
		super(name, jdbcType);
	}
	
	/**
	 * 构造函数
	 * @param name 列的名称
	 * @param jdbcType 在java.sql.Types类中指定类型
	 * @param length 数据库长度
	 */
	public ExtendConstraintColumn(String name , int jdbcType,int length ) {
		super(name, jdbcType, length);
	}

	/**
	 * 构造函数
	 * @param name 列的名称
	 * @param sqlType 在java.sql.Types类中指定类型
	 */
	public ExtendConstraintColumn(String name , SqlType sqlType) {
		super(name, sqlType);
	}
	
	/**
	 * 约束是否添加到column上, true:是  false:否<br />
	 * SQL 92中定义了约束有两种写法,详情请参阅SQL 92的定义,用constraintInline属性来控制这两种书写格式.<br />
	 * @return
	 */
	public boolean isConstraintInline() {
		return constraintInline;
	}
	
	/**
	 * 约束是否添加到column上, true:是  false:否<br />
	 * SQL 92中定义了约束有两种写法,详情请参阅SQL 92的定义,用constraintInline属性来控制这两种书写格式.<br />
	 * @param constraintInline
	 */
	public void setConstraintInline(boolean constraintInline) {
		this.constraintInline = constraintInline;
	}

	/**
	 * 获取字段约束
	 * @return
	 */
	public SqlConstraint getConstraint() {
		return constraint;
	}

	/**
	 * 添加数据库约束
	 * 
	 * @param constraint
	 */
	public ExtendConstraintColumn addSqlConstraint(SqlConstraint constraint ){
		if(  constraint instanceof ConstraintUndefine) return this;
		
		this.constraint.addConstraint(constraint);
		return this;
	}
	
	/**
	 * 添加默认值
	 * @param defaultValue int 类型
	 */
	public ExtendConstraintColumn setDefaultValue( int defaultValue ){		
		constraint.addConstraint(new ConstraintDefault(getSqlType(), defaultValue+"" ));
		return this;
	}
	
	/**
	 * 添加默认值 
	 * @param defaultValue String 类型
	 */
	public ExtendConstraintColumn setDefaultValue( String defaultValue ){		
		constraint.addConstraint(new ConstraintDefault(getSqlType(), defaultValue ));
		return this;
	}
	
	/**
	 * 设定默认值为NULL
	 */
	public ExtendConstraintColumn setDefaultValueIsNull(){
		ConstraintDefault c = new ConstraintDefault(null);
		c.setDefaultValueIsNull();
		constraint.addConstraint(c);
		return this;
	}
	
	/**
	 *设定默认值为 字符串空
	 */
	public ExtendConstraintColumn setDefaultValueIsEmptyString(){
		ConstraintDefault c = new ConstraintDefault(null);
		c.setDefaultValueIsEmptyString();
		constraint.addConstraint(c);
		return this;
	}
	
	/**
	 * 添加不为空的约束
	 */
	public ExtendConstraintColumn setNotNullConstraint(){
		constraint.addConstraint(new ConstraintNotNull());
		return this;
	}
	
	/**
	 * 添加主键约束
	 */
	public ExtendConstraintColumn setPrimarykeyConstraint(){
		constraint.addConstraint(new ConstraintPrimarykey());
		return this;
	}
	
	/**
	 * 添加唯一约束
	 */
	public ExtendConstraintColumn setUniqueConstraint(){
		constraint.addConstraint(new ConstraintUnique());
		return this;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.SqlColumn#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel) throws CompilationException {
		int linelength=50;
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		IndentSpace.newLine(answer);
		IndentSpace.spaceIndent(answer, indentLevel);
		addFormattedSqlDocLines(answer, indentLevel);
		answer.append( formatName (getName().compiledContent(0),10 ));
		if(getSqlType() != null){
			answer.append(getSqlType().compiledContent(0));
		}
		
		if( constraint.isDefaultConstraint() ){
			answer.append( constraint.getDefaultConstraint().compiledContent(0) ) ;
		}
		
		if( constraint.isNotNullConstraint() ){
			answer.append( constraint.getNotNullConstraint().compiledContent(0) ) ;
		}
		
		if( isConstraintInline()){
			if ( constraint.isPrimaryKeyConstraint()  ){
				ConstraintPrimarykey pk =(ConstraintPrimarykey)constraint.getPrimaryKeyConstraint();
				pk.setAddRandomConstraintName(false);
				answer.append( pk.compiledContent(0) )
				;
			}
				
			if( constraint.isUniqueConstraint()){
				ConstraintUnique pk =(ConstraintUnique)constraint.getUniqueConstraint();
				pk.setAddRandomConstraintName(false);
				answer.append( pk.compiledContent(0) )
				;
			}
		}

		//comment , SQL 92中没有定义
		if(StringUtility.stringHasValue(getComment())){
			String []l= answer.toString().split("\n");
			int linesize =l[ l.length -1 ].length();
			if(linesize < linelength){
				IndentSpace.space(answer, linelength - linesize);
			}
			answer.append(SqlKeyWord.COMMENT)
			.append(SqlKeyWord.OPERATION_SINGLE_QUOTES).
			append(getComment()).
			append(SqlKeyWord.OPERATION_SINGLE_QUOTES);
		}
		
		return answer;
	}
}
